package com.eugenema.util.ExcelUtil;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener;
import com.eugenema.entity.FutureMatch;
import com.eugenema.entity.Team;
import com.eugenema.util.SeleniumUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * 未来比赛信息监听器
 * 用于 Excel 数据和实体类的某些属性转换
 * 读取的内容，会存放在全局存储 map（SeleniumUtil.globalMap）中。key 为监听类名
 *
 * @author Eugenema
 * @date 2023/3/5 14:47
 */
public class FutureMatchListener implements ReadListener<FutureMatch> {
    private Logger logger = LoggerFactory.getLogger(FutureMatchListener.class);

    /**
     * 读取到的数据
     */
    private List<FutureMatch> futureMatchList = new ArrayList<>();

    /**
     * 每一条数据解析都会来调用
     *
     * @param data    一行数据
     * @param context 解析内容
     * @author Eugenema
     * @date 2023/3/5 14:54
     **/
    @Override
    public void invoke(FutureMatch data, AnalysisContext context) {
        futureMatchList.add(data);
    }

    /**
     * 所有数据解析完成后调用
     *
     * @param context
     * @author Eugenema
     * @date 2023/3/5 14:59
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<< 所有数据解析完成 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        SeleniumUtil.globalMap.put("FutureMatchListener", futureMatchList);
    }

    /**
     * 对额外信息（批注、超链接、合并单元格信息读取）进行处理
     *
     * @param extra
     * @param context
     * @return
     * @author Eugenema
     * @date 2023/3/5 15:05
     **/
    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        switch (extra.getType()) {
            case COMMENT:
                logger.info("额外信息是批注，在rowIndex:{}，columnIndex：{}，内容是：{}", new Object[]{extra.getRowIndex(), extra.getColumnIndex(), extra.getText()});
                break;
            case HYPERLINK:
                FutureMatch futureMatch = futureMatchList.get(extra.getRowIndex() - 1);
                Team team;
                switch (extra.getColumnIndex()) {
                    case 2:
                        team = futureMatch.getMasterTeam();
                        break;
                    case 4:
                        team = futureMatch.getGuestTeam();
                        break;
                    case 6:
                        team = futureMatch.getTargetTeam();
                        break;
                    default:
                        return;
                }
                team.setLink(extra.getText());
                break;
            case MERGE:
                logger.info("额外信息是超链接，而且覆盖了一个区间，在firstRowIndex：{%s}，firstcolumnIndex：{%s},lastRowIndex：{%s}，lastColumnIndex：{%s}，内容是：{%s}",
                        new Object[]{extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), extra.getLastColumnIndex(), extra.getText()});
                break;
            default:
        }
    }
}
